import { PLAY_LIST_DETAIL } from '../api'
const defaultState = {
  id: 0,
  name: '',//歌单名称
  songs: [],//歌曲列表
  tags: [],//歌曲标签
  coverImgUrl: "",//歌单logo
  updateTime: Date.now(),//更新时间
  count: 0//歌曲数量

}

/**
 * 歌单列表
 */
export default (state = defaultState, action) => {
  switch (action.type) {
    case 'INIT_PLAY_LIST':
      return {
        ...state,
        id: action.id,
        name: action.name,
        songs: action.songs,
        tags: action.tags,
        coverImgUrl: action.coverImgUrl,
        updateTime: action.updateTime,
        count: action.count
      }
    default:
      return state
  }
}


export const loadSongList = (id) => (dispatch) => {

  fetch(PLAY_LIST_DETAIL + id).then(response => {
    return (
      response.json()

    )
  }).then(result => {
    console.log(result)

    dispatch({
      type: 'INIT_PLAY_LIST',
      id: result.playlist.id,
      name: result.playlist.name,
      updateTime: result.playlist.updateTime,
      coverImgUrl: result.playlist.coverImgUrl,
      tags: result.playlist.tags,
      songs: result.playlist.tracks,
      count: result.playlist.tracks.length
    })
  })
}